<template>
  <div class="container">
    <el-card>
      <el-row type="flex">
        <el-col :span="18">
          <el-form>
            <el-form-item label="关键字" label-width="80px">
              <el-input
                v-model="page.keyword"
                style="width: 188px"
                placeholder="根据文章标题搜索"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态" label-width="80px">
              <el-select v-model="state" style="width: 188px">
                <el-option label="启用" value="启用"></el-option>
                <el-option label="禁用" value="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-button size="small" @click="resetForm">清除</el-button>
            <el-button type="primary" size="small" @click="searchList"
              >搜索</el-button
            >
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-row type="flex" justify="end">
            <el-button
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="addArt"
            >新增技巧</el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 15px"
      />
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column label="文章标题">
          <template v-slot="{ row }">
            {{ row.title }}
            <i
              class="el-icon-film"
              v-if="row.videoURL"
              @click="videoPlay(row.videoURL)"
            ></i>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" width="174">
        </el-table-column>
        <el-table-column prop="username" label="录入人" width="172">
        </el-table-column>
        <el-table-column label="录入时间" width="172">
          <template v-slot="{ row }">
            {{ row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template v-slot="{ row }">
            {{ row.state ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column prop="date" label="操作" width="180">
          <template v-slot="{ row, $index }">
            <el-button type="text" @click="artPreview(row.id)">预览</el-button>
            <el-button
              type="text"
              @click="changeState(row.id, row.state, $index)"
              >{{ row.state ? "禁用" : "启用" }}</el-button
            >
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="updateArt(row.id)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="removeArticle(row.id, $index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end">
        <el-pagination
          layout="prev, pager, next, sizes, jumper"
          :total="counts"
          :page-size="page.pagesize"
          :page-sizes="[5,10,20,50]"
          :current-page="page.page"
          hide-on-single-page
          @current-change="pageChange($event)"
          @size-change="pagesizeChange($event)"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <VedioPreview
      :videoURL="videoURL"
      v-if="videoShow"
      :videoShow.sync="videoShow"
    />
    <ArticlePreview
      :artId="artId"
      :previewDialog.sync="previewDialog"
      v-if="previewDialog"
    />
    <ArticleAdd
      :addDialog.sync="addDialog"
      v-if="addDialog"
      :title="title"
      :artId.sync="artId"
      @refresh="getList"
    />
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles'
import VedioPreview from '@/module-hmmm/components/vedio-preview'
import ArticlePreview from '@/module-hmmm/components/articles-preview'
import ArticleAdd from '@/module-hmmm/components/articles-add'
export default {
  name: 'Articles',
  components: {
    VedioPreview, ArticlePreview, ArticleAdd
  },
  data () {
    return {
      list: [], // 文章列表
      page: {
        page: 1, // 当前页码
        pagesize: 10, // 每页条数
        keyword: '', // 搜索关键词
        state: null // 文章状态
      },
      counts: 0,
      state: null, // 文章状态
      videoURL: null, // 视频预览的地址
      videoShow: false, // 视频显示
      previewDialog: false, // 文章预览界面显示
      artId: null, // 文章ID
      addDialog: false, // 修改添加文章弹层显示
      title: null // 修改、添加文章的标题
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data: { counts, items } } = await list(this.page)
      this.list = items
      this.counts = counts
    },
    resetForm () {
      this.page.keyword = ''
      this.page.state = null
      this.state = null
    },
    searchList () {
      if (this.state === '启用') {
        this.page.state = 1
      } else if (this.state === '禁用') {
        this.page.state = 0
      }
      this.getList()
    },
    videoPlay (url) {
      this.videoURL = url
      this.videoShow = true
    },
    async changeState (id, state, index) {
      try {
        await changeState({ id, state: state ? 0 : 1 })
        this.list[index].state = state ? 0 : 1
        this.$message.success('操作成功')
      } catch (err) {
        console.log(err)
        this.$message.error('操作失败，请重试')
      }
    },
    async removeArticle (id, index) {
      try {
        await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await remove({ id })
        this.getList()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
        this.$message.error('操作失败，请重试')
      }
    },
    pageChange (page) {
      this.page.page = page
      this.getList()
    },
    artPreview (id) {
      this.artId = id
      this.previewDialog = true
    },
    updateArt (id) {
      this.artId = id
      this.title = '修改文章'
      this.addDialog = true
    },
    addArt () {
      this.title = '新增文章'
      this.addDialog = true
    },
    pagesizeChange (e) {
      this.page.pagesize = e
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  padding: 10px;
  .el-form-item {
    display: inline-block;
    margin: 0 10px 18px 0;
  }
  .el-select-dropdown__item.selected {
    color: #409eff;
    font-weight: 700;
  }
  .el-icon-film {
    color: #00f;
    font-size: 18px;
  }
}
</style>
